<?php
require(dirname(dirname(__FILE__))."/Config/config.php");
require(dirname(dirname(__FILE__))."/jssdk.php");
class signController extends appController{
	public $wxid = 0;//数据库Id
	public $m;//自己的model
	public $info=array();//当前用户的微信信息
	
	public $maxSignED = 2;
	public $maxSign = 15;
	public $spacing = 3600;//秒
	public  function __construct(){
		parent::__construct();

		//基础信息构建

		$this->m = Model('sign');
		//微信分享参数构建
		$jsdk = $this->m ->get_jsdk();

		$this->assign("signPackage", $jsdk);

		//当前用户的微信id
		$this->wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;

		if($this->wxid>0)
		{
			//当前用户的微信信息
			$this->info = $this->m->get_record($this->wxid);
			$this->assign('info',$this->info);
		}
	}

	public function set()
	{
		$_SESSION['wxid']=$_GET['wxid'];
	}
	//清除数据
	public function clear()
	{

		unset($_SESSION['wxid']);
		unset($_SESSION['pid']);
		echo "已清除数据";
		exit;
	}
	//需要授权才可进入游戏
	public function canIn()
	{
		if(!$this->info)
		{
			header("Location:http://".$_SERVER['HTTP_HOST']."/ws/index.php?g=home&m=sign&a=index");
			exit;
		}
	}
	//微信访问地址
	public function index(){

		if(!isset($_SESSION['wxid']))
		{
			$s = Model('app');
			$app = $s->get_app();
			
			if(!isset($_GET['code']))
			{
				$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
				$url = $this->parseurl($url);
				header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$app['appid'].'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect');
			}
			else
			{
				
				$access_token = $s->getAccessToken();
				$weiXinObj = new weixinController();
				$code = $_GET['code'];
				$arr = $weiXinObj->getUserByCode($app['appid'], $app['appsecret'], $code);
				$check_openid = $s->get_count_openid($arr['openid']);
				if($check_openid == 0)
				{
					$str = $weiXinObj->getUserInfo($arr['openid'], $arr['access_token']);
					$info['openid'] = $str['openid'];
					$info['nickname'] = $str['nickname'];
					$info['sex'] = $str['sex'];
					$info['headimgurl'] = $str['headimgurl'];
					$wxid =$s->insert_into_wxuser($info);
					if(!$wxid)
					{
						echo "System Error! Please wait for a while!";
					}
				}
				else
				{
					$wxid = $s->get_wxid($arr['openid']);
				}
				$this->m->insert_record($wxid);
				$_SESSION['wxid'] = $wxid;
				header('Location:http://'.$_SERVER['HTTP_HOST'].'/ws/index.php?g=mobile&m=sign&a=index');
			}
		
		}else{
			$config[c] = array('site_name' => "绿色出行摇一摇-".Model('app')->get_cfg('site_name'));
			
			$this->assign_global($config);
			$record = $this->m->get_sign_record($this->wxid);
			$this->assign("record", $record);
			$result = $this->m->get_isSign_isPersent($this->wxid,$this->maxSignED,$this->maxSign,$this->spacing);
			//判断是否能够签到
			$this->assign("isSign", $result['isSign']);
			//判断是否能够领取礼品
			$this->assign("isPersent", $result['isPersent']);
			$this->assign("persent", $result['persent']);
			$this->display('index.html');
		}

	}

	function parseurl($url="")
	{
		$url = rawurlencode(mb_convert_encoding($url, 'gb2312', 'utf-8'));

		return $url;
	}
	public function sign(){
		$this->canIn();
		$result=array('error'=>0,'content'=>'签到失败',"isFirst"=>false);
		$data['wxid'] = $this->wxid;
		$res =$this->m->sign($data,$this->maxSignED,$this->maxSign,$this->spacing);
		print_r(json_encode($res));
	}
	public function persent(){
		$this->canIn();
		$result =array("error"=>0,"content"=>"该奖品您已领取");
		$pid = isset($_POST['pid'])?intval($_POST['pid']):0;
		if($pid){
			$result = $this->m->persent($this->wxid,$pid);
		}
		print_r(json_encode($result));
	}
}